package 力扣91;

/**
 * @ClassName : Day_15_129_求根节点到叶节点数字之和
 * @Author : 骆发茂
 * @Date: 2021/12/26 10:43
 * @Description : https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/
 * https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/solution/0-ms-jiao-ke-shu-ji-jie-da-by-liuzhaoce/
 */
public class Day_15_129_求根节点到叶节点数字之和_dfs {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public int sumNumbers(TreeNode root) {
            return dfs(root, 0);

        }
        private int dfs(TreeNode root, int preSum) {
            if (root == null) {
                return 0;
            }
            preSum = preSum * 10 + root.val;
            if (root.left == null && root.right == null) {
                return preSum;
            }

            return dfs(root.left, preSum) + dfs(root.right, preSum);
        }
    }



}